﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarGenderDB
/// </summary>
/// 
namespace CE.Provider
{

    public class CarGenderDB
    {
        public CarGenderDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarGender CarGender)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarGenderInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderGenderId", CarGender.ProviderGenderId);
            _sPara.Add("@ProviderCompanyId", CarGender.ProviderCompanyId);
            _sPara.Add("@Gender", CarGender.Gender);      
            _sPara.Add("@Rate", CarGender.Rate);
            _sPara.Add("@RefCEUserId", CarGender.RefCEUserId);
            _sPara.Add("@Status", CarGender.Status);
            _sPara.Add("@Finfo1", CarGender.FInfo1);
            _sPara.Add("@Dinfo1", CarGender.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 ProviderCarGenderId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarGenderDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderGenderId", ProviderCarGenderId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public CarGenderList GetProviderCarGenderList(Int64 ProviderCompanyId)
        {
            CarGenderList _CarGenderlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarGenderSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _CarGenderlist = new CarGenderList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarGender _CarGender = new CarGender();
                        _CarGender = FillDataRecord(_dt, i);
                        _CarGenderlist.Add(_CarGender);
                    }
                }
            }
            return _CarGenderlist;
        }


        public CarGender GetProviderCarGender(Int64 ProviderCompanyId, string Sex)
        {
            CarGender _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarGenderSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@Sex", Sex);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarGender();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarGender GetProviderCarGender(Int64 ProviderCarGenderId)
        {
            CarGender _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarGenderSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderGenderId", ProviderCarGenderId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarGender();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarGender FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarGender myCarGender = new CarGender();
            myCarGender.ProviderGenderId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderGenderId"].ToString());
            myCarGender.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarGender.Gender = myDataTable.Rows[RowId]["Gender"].ToString();
            myCarGender.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarGender.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarGender.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarGender.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarGender.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarGender.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarGender.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarGender;
        }
    }
}
